/*
 * File: look2_iu8lu16n16tu16_binlcase.c
 *
 * Code generated for Simulink model 'TABL'.
 *
 * Model version                  : 3.213
 * Simulink Coder version         : 9.4 (R2020b) 29-Jul-2020
 * C/C++ source code generated on : Thu Apr 28 14:14:16 2022
 */

#include "rtwtypes.h"
#include "look2_iu8lu16n16tu16_binlcase.h"

uint16_T look2_iu8lu16n16tu16_binlcase(uint8_T u0, uint8_T u1, const uint8_T
    bp0[], const uint8_T bp1[], const uint16_T table[], const uint32_T maxIndex[],
    uint32_T stride)
{
    uint32_T bpIndices[2];
    uint32_T bpIdx;
    uint32_T iLeft;
    uint32_T iRght;
    uint16_T fractions[2];
    uint16_T frac;
    uint16_T y;
    uint16_T yL_0d0;
    uint16_T yR_0d0;
    uint8_T bpLeftVar;

    /* Column-major Lookup 2-D
       Search method: 'binary'
       Use previous index: 'off'
       Interpolation method: 'Linear point-slope'
       Extrapolation method: 'Clip'
       Use last breakpoint for index at or above upper limit: 'on'
       Remove protection against out-of-range input in generated code: 'off'
       Rounding mode: 'simplest'
     */
    /* Prelookup - Index and Fraction
       Index Search method: 'binary'
       Extrapolation method: 'Clip'
       Use previous index: 'off'
       Use last breakpoint for index at or above upper limit: 'on'
       Remove protection against out-of-range input in generated code: 'off'
       Rounding mode: 'simplest'
     */
    if (u0 <= bp0[0U])
    {
        iLeft = 0U;
        frac = 0U;
    }
    else if (u0 < bp0[maxIndex[0U]])
    {
        /* Binary Search */
        bpIdx = maxIndex[0U] >> 1U;
        iLeft = 0U;
        iRght = maxIndex[0U];
        while (iRght - iLeft > 1U)
        {
            if (u0 < bp0[bpIdx])
            {
                iRght = bpIdx;
            }
            else
            {
                iLeft = bpIdx;
            }

            bpIdx = (iRght + iLeft) >> 1U;
        }

        bpLeftVar = bp0[iLeft];
        frac = (uint16_T)(((uint32_T)(uint8_T)((uint32_T)u0 - bpLeftVar) << 16) /
                          (uint8_T)((uint32_T)bp0[iLeft + 1U] - bpLeftVar));
    }
    else
    {
        iLeft = maxIndex[0U];
        frac = 0U;
    }

    fractions[0U] = frac;
    bpIndices[0U] = iLeft;

    /* Prelookup - Index and Fraction
       Index Search method: 'binary'
       Extrapolation method: 'Clip'
       Use previous index: 'off'
       Use last breakpoint for index at or above upper limit: 'on'
       Remove protection against out-of-range input in generated code: 'off'
       Rounding mode: 'simplest'
     */
    if (u1 <= bp1[0U])
    {
        iLeft = 0U;
        frac = 0U;
    }
    else if (u1 < bp1[maxIndex[1U]])
    {
        /* Binary Search */
        bpIdx = maxIndex[1U] >> 1U;
        iLeft = 0U;
        iRght = maxIndex[1U];
        while (iRght - iLeft > 1U)
        {
            if (u1 < bp1[bpIdx])
            {
                iRght = bpIdx;
            }
            else
            {
                iLeft = bpIdx;
            }

            bpIdx = (iRght + iLeft) >> 1U;
        }

        bpLeftVar = bp1[iLeft];
        frac = (uint16_T)(((uint32_T)(uint8_T)((uint32_T)u1 - bpLeftVar) << 16) /
                          (uint8_T)((uint32_T)bp1[iLeft + 1U] - bpLeftVar));
    }
    else
    {
        iLeft = maxIndex[1U];
        frac = 0U;
    }

    /* Column-major Interpolation 2-D
       Interpolation method: 'Linear point-slope'
       Use last breakpoint for index at or above upper limit: 'on'
       Rounding mode: 'simplest'
       Overflow mode: 'wrapping'
     */
    bpIdx = iLeft * stride + bpIndices[0U];
    if (bpIndices[0U] == maxIndex[0U])
    {
        y = table[bpIdx];
    }
    else
    {
        yR_0d0 = table[bpIdx + 1U];
        yL_0d0 = table[bpIdx];
        if (yR_0d0 >= yL_0d0)
        {
            y = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)(uint16_T)((uint32_T)
                              yR_0d0 - yL_0d0) * fractions[0U]) >> 16) + yL_0d0);
        }
        else
        {
            y = (uint16_T)((uint32_T)yL_0d0 - (uint16_T)(((uint32_T)(uint16_T)
                             ((uint32_T)yL_0d0 - yR_0d0) * fractions[0U]) >> 16));
        }
    }

    if (iLeft == maxIndex[1U])
    {
    }
    else
    {
        bpIdx += stride;
        if (bpIndices[0U] == maxIndex[0U])
        {
            yR_0d0 = table[bpIdx];
        }
        else
        {
            yR_0d0 = table[bpIdx + 1U];
            yL_0d0 = table[bpIdx];
            if (yR_0d0 >= yL_0d0)
            {
                yR_0d0 = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)(uint16_T)
                                      ((uint32_T)yR_0d0 - yL_0d0) * fractions[0U])
                                     >> 16) + yL_0d0);
            }
            else
            {
                yR_0d0 = (uint16_T)((uint32_T)yL_0d0 - (uint16_T)(((uint32_T)
                                      (uint16_T)((uint32_T)yL_0d0 - yR_0d0) *
                                      fractions[0U]) >> 16));
            }
        }

        if (yR_0d0 >= y)
        {
            y = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)(uint16_T)((uint32_T)
                              yR_0d0 - y) * frac) >> 16) + y);
        }
        else
        {
            y = (uint16_T)((uint32_T)y - (uint16_T)(((uint32_T)(uint16_T)
                             ((uint32_T)y - yR_0d0) * frac) >> 16));
        }
    }

    return y;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
